'use strict';

const Controller = require('egg').Controller;

class LoginController extends Controller {
  // 客户端登录
  async index () {
    const data = this.ctx.query;
    const { ctx } = this;
    if (Object.prototype.isPrototypeOf(data) && Object.keys(data).length === 0) {
      ctx.body = '登录失败，请输入账号密码';
    } else {
      const res = await ctx.service.login.login(data);
      ctx.body = res;
    }
  }

  // 退出登录，删除cookies
  async exit () {
    const ctx = this.ctx;
    ctx.cookies.set('user', null);
    ctx.body = {
      status: 200,
      data: '删除Cookies成功',
    };
  }

  // 退出登录，删除cookies
  async exit2 () {
    const ctx = this.ctx;
    ctx.cookies.set('user213', 'test');
    ctx.body = {
      status: 200,
      data: 'Cookies213成功',
    };
  }
  // 后台管理员登录
  async admin_login () {
    const { ctx } = this;
    const data = ctx.request.body;
    console.log('管理员登录：', data);
    if (Object.prototype.isPrototypeOf(data) && Object.keys(data).length === 0) {
      ctx.body = '登录失败';
      // return '登录失败';
    } else {
      const res = await ctx.service.login.admin_login(data);
      ctx.body = res;
    }
  }
  async admin_getuserinfo () {
    const data = this.ctx.query;
    console.log('admin_getuserinfo', data);
    const { ctx } = this;
    const res = await ctx.service.login.admin_getuserinfo(data);
    ctx.body = res;

  }

}

module.exports = LoginController;
